package com.example.anydo.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.anydo.R;
import com.example.anydo.activity.EditTaskActivity;
import com.example.anydo.activity.TomatoAlarmActivity;
import com.example.anydo.bean.TaskBean;
import com.example.anydo.util.database.DatabaseHelper;
import com.example.anydo.util.pub_util.SPUtil;
import com.example.anydo.util.pub_util.Util;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

public class TaskAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<TaskBean> taskBeanList ;
    Context context;
    DatabaseHelper helper;

    // task的类型
    private static final int TYPE_EASY = 1;
    private static final int TYPE_NORMAL = 2;
    private static final int TYPE_INCLUDING_SUBTASK = 3;
    private Integer userId;

    public TaskAdapter(List<TaskBean> taskBeanList, Context context){
        this.taskBeanList = taskBeanList;
        this.context = context;
        helper = new DatabaseHelper(context);
        userId = Integer.parseInt(SPUtil.getUserId(context));
    }

    public void freshData(List<TaskBean> taskBeanList){
        this.taskBeanList = taskBeanList;
        notifyDataSetChanged();
    }

    private void jumpActivity(String title){
        Intent intent = new Intent(context, TomatoAlarmActivity.class);
        intent.putExtra("title",title);
        context.startActivity(intent);
    }

    static class EasyViewHolder extends RecyclerView.ViewHolder{
        View view;
        CheckBox checkBoxIsFinished;
        TextView textMsg;
        ImageView img_clock;

        public EasyViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            this.view = itemView;
            this.checkBoxIsFinished = itemView.findViewById(R.id.checkbox_finished);
            this.textMsg = itemView.findViewById(R.id.txt_msg);
            this.img_clock = itemView.findViewById(R.id.img_clock);
        }
    }

    static class NormalViewHolder extends RecyclerView.ViewHolder{
        View view;
        CheckBox checkBoxIsFinished;
        TextView textMsg;
        TextView textRemindTime;
        TextView textRepeated;
        ImageView img_clock;


        public NormalViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            this.view = itemView;
            this.checkBoxIsFinished = itemView.findViewById(R.id.checkbox_finished);
            this.textMsg = itemView.findViewById(R.id.txt_msg);
            this.textRemindTime = itemView.findViewById(R.id.txt_remind_time);
            this.textRepeated = itemView.findViewById(R.id.txt_repeated_task);
            this.img_clock = itemView.findViewById(R.id.img_clock);

        }
    }

    static class IncludingSubtaskViewHolder extends RecyclerView.ViewHolder{
        View view;
        CheckBox checkBoxIsFinished;
        TextView textMsg;
        TextView textRemindTime;
        TextView textRepeated;
        ImageView img_clock;
        RecyclerView recyclerSubTask;

        public IncludingSubtaskViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            this.view = itemView;
            this.checkBoxIsFinished = itemView.findViewById(R.id.checkbox_finished);
            this.textMsg = itemView.findViewById(R.id.txt_msg);
            this.textRemindTime = itemView.findViewById(R.id.txt_remind_time);
            this.textRepeated = itemView.findViewById(R.id.txt_repeated_task);
            this.img_clock = itemView.findViewById(R.id.img_clock);
            this.recyclerSubTask = itemView.findViewById(R.id.recycler_subtask);
        }
    }

    @Override
    public int getItemViewType(int position) {
        TaskBean.DataBean dataBean = taskBeanList.get(position).getData();
        if(dataBean.getSubTasks() == null || dataBean.getSubTasks().size() == 0){
            if((dataBean.getRate() == null || dataBean.getRate().length() == 0)
                    && (dataBean.getTime() == null || dataBean.getTime().length() == 0)){
                return TYPE_EASY;
            }else{
                return TYPE_NORMAL;
            }
        }else{
            return TYPE_INCLUDING_SUBTASK;
        }
    }

    @NonNull
    @NotNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        if(viewType == TYPE_EASY){
            View view = LayoutInflater.from(context).inflate(R.layout.task_item_easy, parent, false);
            return new EasyViewHolder(view);
        }else if(viewType == TYPE_NORMAL){
            View view = LayoutInflater.from(context).inflate(R.layout.task_item_normal, parent, false);
            return new NormalViewHolder(view);
        }else{
            View view = LayoutInflater.from(context).inflate(R.layout.task_item_including_subtask, parent, false);
            return new IncludingSubtaskViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull final RecyclerView.ViewHolder holder, final int position) {

        // item 点击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditTaskActivity.actionStart(context,taskBeanList.get(position),EditTaskActivity.SAVE_UPDATE);
            }
        });

        final TaskBean.DataBean dataBean = taskBeanList.get(position).getData();
        if(holder instanceof EasyViewHolder){
            if(dataBean.getIfDone() != null){
                ((EasyViewHolder)holder).checkBoxIsFinished.setChecked(dataBean.getIfDone().equals("是"));
                ((EasyViewHolder)holder).checkBoxIsFinished.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                    // checkbox 点击
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                        ((EasyViewHolder)holder).textMsg.setTextColor(b ? context.getResources().getColor(R.color.green) : context.getResources().getColor(R.color.black));
                        if(b){
                            ((EasyViewHolder) holder).img_clock.setVisibility(View.INVISIBLE);
                        }else {
                            ((EasyViewHolder) holder).img_clock.setVisibility(View.VISIBLE);
                        }

                        // 数据缓存
                        helper.setTaskState(dataBean.getId(),userId,b);
                    }
                });
            }
            if(dataBean.getTitle() != null){
                ((EasyViewHolder)holder).textMsg.setTextColor(((EasyViewHolder)holder).checkBoxIsFinished.isChecked() ? context.getResources().getColor(R.color.green) : context.getResources().getColor(R.color.black));
                ((EasyViewHolder)holder).textMsg.setText(dataBean.getTitle());
            }
            //显示、隐藏专注按钮
            if(((EasyViewHolder) holder).checkBoxIsFinished.isChecked()){
                ((EasyViewHolder) holder).img_clock.setVisibility(View.INVISIBLE);
            }else {
                ((EasyViewHolder) holder).img_clock.setVisibility(View.VISIBLE);
            }
            // 跳转到专注界面
            ((EasyViewHolder) holder).img_clock.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    jumpActivity(dataBean.getTitle());
                }
            });
        }
        if(holder instanceof NormalViewHolder){
            if(dataBean.getIfDone() != null){
                ((NormalViewHolder)holder).checkBoxIsFinished.setChecked(dataBean.getIfDone().equals("是"));
                ((NormalViewHolder)holder).checkBoxIsFinished.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                        ((NormalViewHolder)holder).textMsg.setTextColor(b ? context.getResources().getColor(R.color.green) : context.getResources().getColor(R.color.black));
                        if(b){
                            ((NormalViewHolder) holder).img_clock.setVisibility(View.INVISIBLE);
                        }else {
                            ((NormalViewHolder) holder).img_clock.setVisibility(View.VISIBLE);
                        }

                        // 数据缓存
                        helper.setTaskState(dataBean.getId(),userId,b);
                    }
                });
            }
            if(dataBean.getTitle() != null){
                ((NormalViewHolder)holder).textMsg.setTextColor(((NormalViewHolder)holder).checkBoxIsFinished.isChecked() ? context.getResources().getColor(R.color.green) : context.getResources().getColor(R.color.black));
                ((NormalViewHolder)holder).textMsg.setText(dataBean.getTitle());
            }


            // 提醒时间提醒
            if(dataBean.getTime() == null || dataBean.getTime().equals("")){
                ((NormalViewHolder)holder).textRemindTime.setVisibility(View.GONE);
            }else {
                ((NormalViewHolder)holder).textRemindTime.setVisibility(View.VISIBLE);
                String time = Util.getTime(dataBean.getTime());
                ((NormalViewHolder)holder).textRemindTime.setText(time+"提醒");
            }

            // 重复事件提醒
            if(dataBean.getRate() == null || dataBean.getRate().equals("")){
                ((NormalViewHolder)holder).textRepeated.setVisibility(View.GONE);
            }else {
                ((NormalViewHolder)holder).textRepeated.setVisibility(View.VISIBLE);
                ((NormalViewHolder)holder).textRepeated.setText("重复事件");
            }

            // 显示、隐藏专注按钮
            if(((NormalViewHolder) holder).checkBoxIsFinished.isChecked()){
                ((NormalViewHolder) holder).img_clock.setVisibility(View.INVISIBLE);
            }else {
                ((NormalViewHolder) holder).img_clock.setVisibility(View.VISIBLE);
            }

            // 跳转到专注界面
            ((NormalViewHolder)holder).img_clock.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    jumpActivity(dataBean.getTitle());
                }
            });

        }
        if(holder instanceof IncludingSubtaskViewHolder){
            if(dataBean.getIfDone() != null){
                ((IncludingSubtaskViewHolder)holder).checkBoxIsFinished.setChecked(dataBean.getIfDone().equals("是"));
                ((IncludingSubtaskViewHolder)holder).checkBoxIsFinished.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                        ((IncludingSubtaskViewHolder)holder).textMsg.setTextColor(b ? context.getResources().getColor(R.color.green) : context.getResources().getColor(R.color.black));
                        if(b){
                            ((IncludingSubtaskViewHolder) holder).img_clock.setVisibility(View.INVISIBLE);
                        }else {
                            ((IncludingSubtaskViewHolder) holder).img_clock.setVisibility(View.VISIBLE);
                        }

                        // 数据缓存
                        helper.setTaskState(dataBean.getId(),userId,b);
                    }
                });
            }
            if(dataBean.getTitle() != null){
                ((IncludingSubtaskViewHolder)holder).textMsg.setTextColor(((IncludingSubtaskViewHolder)holder).checkBoxIsFinished.isChecked() ? context.getResources().getColor(R.color.green) : context.getResources().getColor(R.color.black));
                ((IncludingSubtaskViewHolder)holder).textMsg.setText(dataBean.getTitle());
            }

            // 提醒时间提醒
            if(dataBean.getTime() == null || dataBean.getTime().equals("")){
                ((IncludingSubtaskViewHolder)holder).textRemindTime.setVisibility(View.GONE);
            }else {
                ((IncludingSubtaskViewHolder)holder).textRemindTime.setVisibility(View.VISIBLE);

                String time = dataBean.getTime();
                ((IncludingSubtaskViewHolder)holder).textRemindTime.setText(time+"提醒");
            }

            // 重复事件提醒
            if(dataBean.getRate() == null || dataBean.getRate().equals("")){
                ((IncludingSubtaskViewHolder)holder).textRepeated.setVisibility(View.GONE);
            }else {
                ((IncludingSubtaskViewHolder)holder).textRepeated.setVisibility(View.VISIBLE);
                ((IncludingSubtaskViewHolder)holder).textRepeated.setText("重复事件");
            }

            if(dataBean.getSubTasks() != null && dataBean.getSubTasks().size() != 0){
                SubTaskAdapter subTaskAdapter = new SubTaskAdapter(dataBean.getSubTasks(), context);
                ((IncludingSubtaskViewHolder)holder).recyclerSubTask.setAdapter(subTaskAdapter);
                RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(context);
                ((IncludingSubtaskViewHolder)holder).recyclerSubTask.setLayoutManager(layoutManager);
            }

            // 显示、隐藏专注按钮
            if(((IncludingSubtaskViewHolder) holder).checkBoxIsFinished.isChecked()){
                ((IncludingSubtaskViewHolder) holder).img_clock.setVisibility(View.INVISIBLE);
            }else {
                ((IncludingSubtaskViewHolder) holder).img_clock.setVisibility(View.VISIBLE);
            }

            // 跳转到专注界面
            ((IncludingSubtaskViewHolder)holder).img_clock.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    jumpActivity(dataBean.getTitle());
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return taskBeanList.size();
    }
}
